package com.miguo.framework.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.miguo.framework.utils.Network;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;

import java.io.File;

public class LazyImageView extends ImageView implements OnClickListener {

    private Context mContext;
    private ImageLoader imageLoader;
    private boolean isNoImgMode;
    private OnClickListener listener;
    private DisplayImageOptions mOptions;
    private String uri;

    public LazyImageView(Context context) {
        super(context);
        init(context);
    }

    public LazyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LazyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        imageLoader = ImageLoader.getInstance();
    }

    public boolean isNoImgMode() {
        return isNoImgMode;
    }

    public void setNoImgMode(boolean noImgMode) {
        isNoImgMode = noImgMode;
    }

    public void autoDisplayImage(String uri, DisplayImageOptions options) {
        autoDisplayImage(uri, options, null);
    }

    public void autoDisplayImage(String imageUri, DisplayImageOptions options,
                                 OnClickListener listeners) {
        uri = imageUri;
        mOptions = options;
        listener = listeners;
        if ((isNoImgMode) && (Network.is2Or3GNet(mContext))) {
            if (isImagEmpty()) {
                imageLoader.displayImage(null, this, options);
                setOnClickListener(this);
                return;
            }
        }
        imageLoader.displayImage(uri, this, options);
    }

    public void manualDisplayImage(String uri, DisplayImageOptions options) {
        imageLoader.displayImage(uri, this, options);
    }

    public boolean isImagEmpty() {
        File file = DiskCacheUtils.findInCache(uri, imageLoader.getDiskCache());
        if ((file == null) || (!file.exists())) {
            return true;
        } else {
            return false;
        }
    }

    public void onClick(View v) {
        if (isImagEmpty()) {
            manualDisplayImage(uri, mOptions);
            return;
        }
        listener.onClick(v);
    }

    // private ImageLoadingListener defaultImageLoadingListener = new
    // ImageLoadingListener() {
    //
    // public void onLoadingStarted(String imageUri, View view) {
    // }
    //
    // public void onLoadingFailed(String imageUri, View view,
    // FailReason failReason) {
    // }
    //
    // public void onLoadingComplete(String imageUri, View view,
    // Bitmap loadedImage) {
    // }
    //
    // public void onLoadingCancelled(String imageUri, View view) {
    // }
    // };

}